
#ifndef __RSA_CHECKING_H
#define __RSA_CHECKING_H

#include "CryptoPipeBlock.h"
#include "../aprtools/APRBucketBrigade.h"
#include <openssl/rsa.h>
#include <openssl/evp.h>
#include "RSAPublicKey.h"

/** Class encapsulating an interface to check data signatures with an
 * RSA public key. */
class RSAChecking : public CryptoPipeBlock
{
   public:
      RSAChecking(const RSAPublicKey &rsapubkey);
      RSAChecking(APRBucketBrigade &iinput, APRBucketBrigade &ioutput, const RSAPublicKey &rsapubkey);
      ~RSAChecking();
      // Check data block and return signed data block
      char *processDataBlock(const char *signedblock, apr_size_t signedsize, apr_size_t *datasize);
      // Begin checking of the data pipe signature
      int processPipeBegin();
      // Step through the checking of the data pipe signature
      int processPipeStep();
      // Step through the data, recording data movement
      int processPipeStep(int *movementin, int *movementout);
      // End checking of the data pipe
      int processPipeEnd();
      /** Was the process secure? */
      int processWasSecure() { return verified; }
      /** Remove the key given from the checking structure. */
      void removeKey() { RSA_free(key); key = NULL; }
   private:
      /** A copy of the RSA private key. */
      RSA *key;
      /** The input bucket brigade. As this is pointer to the outside,
       * it is not deleted in the destructor. */
      APRBucketBrigade *input;
      /** The output bucket brigade. As this is pointer to the
       * outside, it is not deleted in the destructor. */
      APRBucketBrigade *output;
      /** Final data block length. */
      int final_data_length;
      /** Secure. */
      int verified;
      /** The message digest context which holds the state of the
       * digest. */
      EVP_MD_CTX e_ctxt;
};

#endif
